﻿using DataAccess;
using KhoaLuan.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KhoaLuan.MainSys.Manager.Controls
{
    public partial class ctChangePermission : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var groupid = Request.QueryString["GroupID"];
                if (groupid != null)
                {
                    var proxy = new tbPowers_Proxy();
                    var proxymenu = new tbMenu_Proxy();
                    var proxygroup = new tbGroup_Proxy();
                    var listmenu = proxymenu.SelectAll_tbMenu_Proxy().ToList<tbMenu>();//.Where(x => x.Link != null && x.Link.Trim() != "");
                    var listpower = proxy.GetByGroupID(int.Parse(groupid));
                    var group = proxygroup.GetById_tbGroup_Proxy(int.Parse(groupid)).ToObject<tbGroup>();
                    lbGroup.InnerHtml = "THÔNG TIN PHÂN QUYỀN CHO GROUP: <b>" + group.GroupName.ToUpper() + "</b>";
                    var listobject = new List<object>();
                    foreach (var item in listmenu)
                    {
                        bool view = false, add = false, edit = false, del = false;
                        int powerid = 0;
                        var power = listpower.Find(x => x.MenuID == item.MenuID);

                        if (power != null)
                        {
                            powerid = power.PowersID;
                            view = SecurityServices.HasPermison(TypeAudit.View, power.NumberPermison);
                            add = SecurityServices.HasPermison(TypeAudit.AddNew, power.NumberPermison);
                            edit = SecurityServices.HasPermison(TypeAudit.Edit, power.NumberPermison);
                            del = SecurityServices.HasPermison(TypeAudit.Delete, power.NumberPermison);
                        }
                        listobject.Add(new
                        {
                            PowerID = powerid,
                            MenuID = item.MenuID,
                            MenuName = item.MenuName,
                            Link = item.Link == null ? "" : item.Link,
                            View = view,
                            Add = add,
                            Edit = edit,
                            Del = del
                        });
                    }
                    repPermisson.DataSource = listobject;
                    repPermisson.DataBind();

                }
            }

        }
    }
}